package com.zjw.zaiagent.tools;

import cn.hutool.core.io.FileUtil;
import com.zjw.zaiagent.constant.FIleConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * @author zjw
 * @date 2025年07月16日 20:15
 */
public class FileOperationTools {

    private static final String FILE_PATH = FIleConstant.FILE_SAVE_DIR + "/file";

    /**
     * 读取文件
     * @param fileName
     * @return
     */
    @Tool(description = "Read content from a file")
    public static String readFile(@ToolParam(description = "name of a file to read") String fileName) {
        try {
            String filePath = FILE_PATH + "/" + fileName;
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "file read is error" + e.getMessage();
        }
    }


    /**
     * 写入文件
     * @param fileName
     * @param content
     */
    @Tool(description = "Write content to a file")
    public static String writeFile(@ToolParam(description = "Name of the file to write") String fileName,
                                   @ToolParam(description = "Content to write to the file") String content) {
        String filePath = FILE_PATH + "/" + fileName;
        try {
            // 创建目录
            FileUtil.mkdir(FILE_PATH);
            FileUtil.writeUtf8String(content, filePath);
            return "write a file is success" + filePath;
        } catch (Exception e) {
            return "write a file is error" + e.getMessage();
        }

    }
}
